﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCDB.Entity;

namespace FCDB.Repository
{
    public class WfActivityInstanceRepository : LogicBase, ILogic
    {
        /// <summary>
        /// 判断有活动实例是否在运行状态
        /// </summary>
        /// <param name="processInstanceID"></param>
        /// <param name="activityGUID"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public WfActivityInstance GetActivityWithRunningState(long processInstanceID, Guid activityGUID, short state)
        {
            var query = from item in DBHelper.WfActivityInstance
                        where item.ActivityGUID == activityGUID && item.ProcessInstanceID == processInstanceID
                        && item.State == state
                        select item;
            if (query.Count() == 1)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }

        public IEnumerable<WfActivityInstance> GetProcessRunningActivityList(long appInstanceID, Guid processGUID)
        {
            //activityState: 1-ready（准备）, 2-running（）运行；
            //  var whereSql = @"WHERE AppInstanceID = @appInstanceID 
            //   AND ProcessGUID = @processGUID
            //     AND (State=1 OR State=2)";
            var query = from item in DBHelper.WfActivityInstance
                        where item.ProcessGUID == processGUID && item.ProcessInstanceID == appInstanceID
                        && (item.State == 1 || item.State == 2)
                        select item;
            return query.ToList();
        }

        public IList<WfActivityInstance> GetAcitivityInstancePaged(int PageIndex, int PageSize, long AppInstanceID, Guid ProcessGUID)
        {
          
            //            var whereSql = @" WHERE AppInstanceID = @appInstanceID
            //                            AND ProcessGUID = @processGUID";
            //            var orderBySql = "ORDER BY CreatedDateTime DESC";

            //var entityList = ActivityInstanceRepository.GetPage<WfActivityInstance>(query.PageIndex, query.PageSize,
            //    whereSql, new
            //    {
            //        appInstanceID = query.AppInstanceID,
            //        processGUID = query.ProcessGUID
            //    },
            //    orderBySql,
            //    session.Connection, session.Transaction);
            var query = from item in DBHelper.WfActivityInstance
                        where item.AppInstanceID == AppInstanceID && item.ProcessGUID == ProcessGUID
                        orderby item.CreatedDateTime descending
                        select item;
            IList<WfActivityInstance> list = query.Skip(PageIndex - 1 * PageSize).Take(PageSize).ToList();
            return list;
        }


        #region 林坚丁
        public IList<WfActivityInstance> GetProcessActivityList(long appInstanceID, Guid processGUID)
        {
            //activityState: 1-ready（准备）, 2-running（）运行；
            //  var whereSql = @"WHERE AppInstanceID = @appInstanceID 
            //   AND ProcessGUID = @processGUID
            //     AND (State=1 OR State=2)";
            var query = from item in DBHelper.WfActivityInstance
                        where item.ProcessGUID == processGUID && item.ProcessInstanceID == appInstanceID
                        select item;
            return query.ToList();
        }
        #endregion 

    }
}
